package com.itheima.web.controller;

import com.itheima.web.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: UserController
 * @Description:
 * @Author liulihui
 * @Date: 2021/8/1 16:53
 * @URL：
 * @Version 1.0
 */
@Controller
@RequestMapping("/user")   //方法中共同的路径可以抽取出来放到类的虚拟路径上
public class UserController {

    @RequestMapping("/hello1")
    //传参参数可以是对象,会自动把前端传过来的参数进行封装
    public String hello1(User user){
        //模拟注册案例
        //能封装对象有前提
        //参数名字 跟对象的set属性名()...
        System.out.println(user);
        return "hell01";
    }


    @RequestMapping("/hello2")
    public String hello2(Model model){
     model.addAttribute("data1","老王");
     model.addAttribute("data2","老刘");
     model.addAttribute("data3","老姐");
     //返回视图地址,也就是jsp模板的位置
        return "/hello1";
    }


    @RequestMapping("/hello3")
    //加上@RequestParam注解封装的原因是防止参数漏被编译,以后都记得加上就行,外面的变量不重要,主要是@RequestParam("username")里面的参数必须和前端传过来的对应上
    public String hello3(@RequestParam("username") String username, @RequestParam("password") String password,@RequestParam("hobbies") List hobbies ){
        System.out.println("获取客户端提交的参数:"+username);
        System.out.println("获取客户端提交的参数:"+ hobbies);
        return "/hello1";
    }
    @RequestMapping("/hello4")
    public String hello4(@RequestParam(value="username") String username,@RequestParam("hobbies") String[] hobbies ){
        System.out.println("获取客户端提交的参数:"+username);
        System.out.println("获取客户端提交的参数:"+ Arrays.toString(hobbies));
        return "/hello1";
    }
    @RequestMapping("/hello5")
    public String hello5(User user){
        //模拟注册案例
        //演示类型转换....date日期

        System.out.println(user);

        //返回视图地址
        return "user/hello1";
    }
    @RequestMapping("/hello6")
    public String hello6(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        //参数部分  除了 前端传递参数
        //普通参数
        //数组
        //list集合
        //直接封装对象 也好

        //还支持一组参数可使用 servlet 体系的

        String username = request.getParameter("username");

        System.out.println("使用传统的request获取:" + username);

        response.setContentType("text/html;charset=utf-8");

        //操作session
        session.setAttribute("data", "放入到session中的数据``````````````");
        Object data = session.getAttribute("data");
        System.out.println("获取session的数据:" + data);


        //返回视图地址
        return "user/hello";

    }

}